package com.rerun.phone.memberuser;

import com.rerun.common.anno.ResponseExceptionReturn;
import com.rerun.common.bean.Rest;
import com.rerun.common.controller.SuperController;
import com.rerun.common.util.Validator;
import com.rerun.entity.MemberUser;
import com.rerun.entity.Resume;
import com.rerun.entity.vo.ResumeVo;
import com.rerun.enums.DataFromEnum;
import com.rerun.service.IMemberUserService;
import com.rerun.service.IResumeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 简历相关接口
 *
 * @author dax.
 * @version v1.0
 * @since 2017 /10/9 9:39
 */
@Api(value = "求职端-简历相关", description = "求职端-简历相关")
@RestController
@RequestMapping("/memberApi/resume")
public class ResumeController extends SuperController {
    @Resource
    private IResumeService iResumeService;
    @Resource
    private IMemberUserService iMemberUserService;

    /**
     * Add resume rest.
     *
     * @param resumeVo the resume vo
     * @return the rest
     */
    @ApiOperation(value = "添加个人简历", notes = "添加个人简历", httpMethod = "POST")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseExceptionReturn
    public Rest addResume(@ModelAttribute ResumeVo resumeVo) {

        String memberId = resumeVo.getMemberId();
        MemberUser user = iMemberUserService.selectById(memberId);
        String headerImg = resumeVo.getHeaderImg();
        String name = resumeVo.getName();
        long sex = resumeVo.getSex();
        Date birthday = resumeVo.getBirthday();
        Date beginWork = resumeVo.getBeginWork();
        String residence = resumeVo.getResidence();
        String livingCity = resumeVo.getLivingCity();
        String phoneNumber = user.getPhone();
        String dataFrom = DataFromEnum.SOCIAL.getType();

        Resume resume = new Resume();
        resume.setMemberId(memberId);
        resume.setHeaderImg(headerImg);
        resume.setName(name);
        resume.setSex(sex);
        resume.setBirthday(birthday);
        resume.setBeginWork(beginWork);
        resume.setResidence(residence);
        resume.setLivingCity(livingCity);
        resume.setPhoneNumber(phoneNumber);
        resume.setDataFrom(dataFrom);

        resume.setStatus(0);
        resume.setCreateTime(new Date());

        boolean flag = iResumeService.insert(resume);
        return flag ? Rest.ok("添加成功") : Rest.failure("添加失败");
    }

    /**
     * Get resume by member id rest.
     *
     * @param memberId the member id
     * @return the rest
     * @deprecated
     */
    @ApiOperation(value = "个人简历", notes = "获取个人简历", httpMethod = "GET")
    @RequestMapping(value = "/get/{memberId}", method = RequestMethod.GET)
    @ResponseExceptionReturn
    public Rest getByMemberId(@ApiParam(required = true, name = "memberId", value = "用户id") @PathVariable("memberId") String memberId) {
        MemberUser user = iMemberUserService.selectById(memberId);
        String phone = user.getPhone();
        return Rest.okData(iResumeService.selectByPhoneNumber(phone));
    }

    /**
     * Update resume rest.
     *
     * @param resumeVo the resume vo
     * @return the rest
     */
    @ApiOperation(value = "更新个人简历", notes = "更新个人简历", httpMethod = "POST")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseExceptionReturn
    public Rest updateResume(@ModelAttribute ResumeVo resumeVo) {
        String memberId = resumeVo.getMemberId();
        Resume resume = iResumeService.selectByMemberId(memberId);

        String headerImg = StringUtils.defaultIfEmpty(resumeVo.getHeaderImg(), resume.getHeaderImg());
        String name = StringUtils.defaultIfEmpty(resumeVo.getName(), resume.getName());
        Long sex = Validator.defaultIfNull(resumeVo.getSex(), resume.getSex());
        Long status = Validator.defaultIfNull(resumeVo.getStatus(), resume.getStatus());
        Date birthday = Validator.defaultIfNull(resumeVo.getBirthday(), resume.getBirthday());
        Date beginWork = Validator.defaultIfNull(resumeVo.getBeginWork(), resume.getBeginWork());
        String residence = StringUtils.defaultIfEmpty(resumeVo.getResidence(), resume.getResidence());
        String livingCity = StringUtils.defaultIfEmpty(resumeVo.getLivingCity(), resume.getLivingCity());
        String dataFrom = StringUtils.defaultIfEmpty(resumeVo.getDataFrom(), resume.getDataFrom());

        resume.setHeaderImg(headerImg);
        resume.setName(name);
        resume.setSex(sex);
        resume.setBirthday(birthday);
        resume.setBeginWork(beginWork);
        resume.setResidence(residence);
        resume.setLivingCity(livingCity);
        resume.setDataFrom(dataFrom);
        resume.setStatus(status);
        resume.setUpdateTime(new Date());
        boolean flag = iResumeService.updateById(resume);
        return flag ? Rest.ok("更新成功") : Rest.failure("更新失败");
    }

    /**
     * Update resume rest.
     *
     * @param memberId the member id
     * @return the rest
     */
    @ApiOperation(value = "获取简历详情", notes = "通过用户ID获取简历详情", httpMethod = "GET")
    @RequestMapping(value = "/detail/{memberId}", method = RequestMethod.GET)
    @ResponseExceptionReturn
    public Rest getResumeDetail(@PathVariable("memberId") String memberId) {

        return Rest.okData(iResumeService.selectResumeDetailByMemberId(memberId));
    }

    /**
     * Update resume rest.
     *
     * @param status   the status
     * @param memberId the member id
     * @return the rest
     */
    @ApiOperation(value = "显示/隐藏简历", notes = "显示/隐藏简历 显示传数字0 隐藏传数字1", httpMethod = "POST")
    @RequestMapping(value = "/showAndHide/{memberId}/{status}", method = RequestMethod.POST)
    @ResponseExceptionReturn
    public Rest resumeShowAndHide(@PathVariable("status") Integer status, @PathVariable("memberId") String memberId) {
        return Rest.okData(iResumeService.resumeShowAndHide(memberId, status));
    }


}
